package com.ja.openchat.ui.chat.remittance;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.widget.AppCompatTextView;

import com.google.gson.JsonObject;
import com.ja.openchat.BR;
import com.ja.openchat.R;
import com.ja.openchat.ui.base.JaBaseActivity;
import com.lxj.xpopup.XPopup;
import com.zy.openchat.core.manager.TelegramManager;
import com.zy.openchat.core.manager.TgResponse;
import com.zy.openchat.core.manager.TgResultBlock;
import com.zy.openchat.core.model.remittance.RemittanceDetailInfo;
import com.zy.openchat.core.model.user.UserInfo;
import com.zy.openchat.util.TimeUtils;

import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

public class TransferMoneyDetailActivity extends JaBaseActivity {

    @BindView(R.id.topIcon)
    ImageView topIcon;
    @BindView(R.id.transferTopLable)
    TextView transferTopLable;
    @BindView(R.id.transferAmountText)
    TextView transferAmountText;
    @BindView(R.id.transferDescritonText)
    TextView transferDescritonText;
    @BindView(R.id.transferTimeText)
    TextView transferTimeText;
    @BindView(R.id.reciveTransferBtn)
    View reciveTransferBtn;
    @BindView(R.id.bottomTextLayout)
    View bottomTextLayout;
    @BindView(R.id.tv_bar_title)
    AppCompatTextView tvBarTitle;
    @BindView(R.id.backTransferBtn)
    View backTransferBtn;
    @BindView(R.id.hintTextLayout)
    View hintTextLayout;



    public static void start(Context context, RemittanceDetailInfo info) {
        Intent intent = new Intent();
        intent.putExtra("transfer",info);
        intent.setClass(context, TransferMoneyDetailActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected boolean isHaveButterKnife() {
        return true;
    }
    @Override
    protected boolean isHaveEventBus() {
        return true;
    }
    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_transfer_money_detail;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    private RemittanceDetailInfo mRemittanceInfo;

    @Override
    public void initView() {
        super.initView();
        tvBarTitle.setText("转账详情");
        if(getIntent()!=null){
            mRemittanceInfo = getIntent().getParcelableExtra("transfer");
        }
        if(mRemittanceInfo==null){
            finish();
            return;
        }

        transferAmountText.setText(String.format("¥%.2f",mRemittanceInfo.amount));
        transferDescritonText.setText(TextUtils.isEmpty(mRemittanceInfo.description)?"":mRemittanceInfo.description);
        transferTimeText.setText(TimeUtils.getTransferFormatTime(mRemittanceInfo.remittedAt*1000));
        load();
    }

    private void load(){
        reciveTransferBtn.setVisibility(View.GONE);
        bottomTextLayout.setVisibility(View.GONE);
        hintTextLayout.setVisibility(View.GONE);
        if(mRemittanceInfo.payerUID == UserInfo.getUserInfo().id){
            UserInfo userInfo = UserInfo.getUserInfo(mRemittanceInfo.payeeUID);
            String userName = "用户";
            if(userInfo!=null){
                userName = userInfo.displayName();
            }else{
                TelegramManager.getTelegramManager().getUserSimpleInfo(mRemittanceInfo.payeeUID, new TgResultBlock<UserInfo>() {
                    @Override
                    public void onResult(Map<String, Object> request, JsonObject response, UserInfo obj) {
                        load();
                    }

                    @Override
                    public void onTimeout(Map<String, Object> request) {

                    }
                });
            }
            if(mRemittanceInfo.status == 0){
                hintTextLayout.setVisibility(View.VISIBLE);
                transferTopLable.setText(String.format("待%s收款",userName));
            }else if(mRemittanceInfo.status==1){
                transferTopLable.setText(String.format("%s已收款",userName));
            }else if(mRemittanceInfo.status==2){
                transferTopLable.setText(String.format("%s已退还",userName));
            }else{
                transferTopLable.setText("系统到期已退还");
            }
        }else{
            String userName = "你";
            if(mRemittanceInfo.status == 0){
                reciveTransferBtn.setVisibility(View.VISIBLE);
                bottomTextLayout.setVisibility(View.VISIBLE);
                transferTopLable.setText(String.format("待%s收款",userName));
            }else if(mRemittanceInfo.status==1){
                transferTopLable.setText(String.format("%s已收款",userName));
            }else if(mRemittanceInfo.status==2){
                transferTopLable.setText(String.format("%s已退还",userName));
            }else{
                transferTopLable.setText("系统到期已退还");
            }
        }

        if(mRemittanceInfo.status == 0){
            topIcon.setImageResource(R.mipmap.transfer_money_detail_icon);
        }else if(mRemittanceInfo.status==1){
            topIcon.setImageResource(R.mipmap.transfer_money_detail_complete_icon);
        }else{
            topIcon.setImageResource(R.mipmap.transfer_money_detail_refund_icon);
        }
    }

    @OnClick({R.id.reciveTransferBtn, R.id.backTransferBtn,R.id.iv_bar_back,R.id.hintTransferBtn})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.reciveTransferBtn:
                doRecive();
                break;
            case R.id.backTransferBtn:
                doRefund();
                break;
            case R.id.iv_bar_back:
                finish();
                break;
            case R.id.hintTransferBtn:
                doHint();
                break;
        }
    }


    private void doRecive(){
        showProgress();
        TelegramManager.getTelegramManager().reciveTransferMoney(this, mRemittanceInfo.remittanceId, new TgResultBlock<TgResponse>() {
            @Override
            public void onResult(Map<String, Object> request, JsonObject response, TgResponse obj) {
                hideProgress();
                if(obj!=null&&obj.ok){
                    showErrorTip("接收转账成功");
                    finish();
                }else if(obj!=null){
                    showErrorTip(obj.errString);
                }
            }

            @Override
            public void onTimeout(Map<String, Object> request) {
                hideProgress();
                showErrorTip("接收转账失败，请稍后重试");
            }
        });
    }


    private void doHint(){
        new XPopup.Builder(this)
                .asCustom(new RefundTranferMoneyDialog(this,"取消" ,"确定",getString(R.string.remind_tranfer_money_text), new OnTransferRefundCallback() {
                    @Override
                    public void doRefund() {
                        showProgress();
                        TelegramManager.getTelegramManager().hintTransferMoney(TransferMoneyDetailActivity.this, mRemittanceInfo.remittanceId, new TgResultBlock<TgResponse>() {
                            @Override
                            public void onResult(Map<String, Object> request, JsonObject response, TgResponse obj) {
                                hideProgress();
                                if(obj!=null&&obj.ok){
                                    showErrorTip("提醒接收转账成功");
                                }else if(obj!=null){
                                    showErrorTip(obj.errString);
                                }
                            }

                            @Override
                            public void onTimeout(Map<String, Object> request) {
                                hideProgress();
                                showErrorTip("提醒接收转账失败，请稍后重试");
                            }
                        });
                    }
                }))
                .show();
    }


    private void doRefund(){
        UserInfo userInfo = UserInfo.getUserInfo(mRemittanceInfo.payerUID);
        if(userInfo==null){
            TelegramManager.getTelegramManager().getUserSimpleInfo(mRemittanceInfo.payerUID, new TgResultBlock<UserInfo>() {
                @Override
                public void onResult(Map<String, Object> request, JsonObject response, UserInfo obj) {
                    if(obj!=null){
                        showRefundDialog(obj.displayName());
                    }else{
                        showErrorTip("退款失败，请稍后重试");
                    }

                }

                @Override
                public void onTimeout(Map<String, Object> request) {

                }
            });
        }else{
            showRefundDialog(userInfo.displayName());
        }

    }

    private void showRefundDialog(String userName){
        new XPopup.Builder(this)
                .asCustom(new RefundTranferMoneyDialog(this, userName, new OnTransferRefundCallback() {
                    @Override
                    public void doRefund() {
                        doRealRefund();
                    }
                }))
                .show();
    }

    private void doRealRefund(){
        showProgress();
        TelegramManager.getTelegramManager().refundTransferMoney(this, mRemittanceInfo.remittanceId, new TgResultBlock<TgResponse>() {
            @Override
            public void onResult(Map<String, Object> request, JsonObject response, TgResponse obj) {
                hideProgress();
                if(obj!=null&&obj.ok){
                    showErrorTip("退款成功");
                    finish();
                }else if(obj!=null){
                    showErrorTip(obj.errString);
                }
            }

            @Override
            public void onTimeout(Map<String, Object> request) {
                hideProgress();
                showErrorTip("退款失败，请稍后重试");
            }
        });
    }
}
